package com.sinodata.bsm.cicp.collector.informix;

import java.sql.ResultSet;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 磁盘排序比 单位 %
 * 磁盘排序增量*100/(磁盘排序增量+内存排序增量)
 * 
 * @author xuxiaoxiao
 *
 */
public class IfxSortsRateCollector extends AbstractJDBCCollector {

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String dbtype = getParamMap().get("DBType");
        String dbname = getParamMap().get("DBName");
        String sql = getParamMap().get("SQL");
        String key = IfxSortsRateCollector.class.getName() + ":" + ip + ":" + dbname + ":" + ":" + dbtype;
        ResultSet rs = null;
        Long tmpmemory = 0L;
        Long tmpdisk = 0L;
        try {
            rs = getCachedResultSet(dbtype, ip, dbname, sql);
            if (rs.next()) {
                tmpmemory = rs.getLong(1);
                tmpdisk = rs.getLong(2);
            }
            String last_value = (String) getPersistentData(key);
            //将采集到得值进行临时保存，保存目录为 agent/save/..
            savePersistentData(key, tmpmemory + "#" + tmpdisk);
            if (last_value != null && tmpmemory >= 0 && tmpdisk >= 0) {
                List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
                String[] propMetas = getPropMeta();
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                //计算出 磁盘排序比
                double rate = 0d;
                String tmpvalues[] = last_value.split("#");
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMaximumFractionDigits(2);

                Long tmpmemoryvalue = tmpmemory - Long.parseLong(tmpvalues[0]);
                Long tmpdiskvalue = tmpdisk - Long.parseLong(tmpvalues[1]);
                Long sum = tmpmemoryvalue + tmpdiskvalue;
                if (sum != 0)
                    rate = 100.00 * tmpdiskvalue / sum;

                rate = rate > 0 ? rate : 0d;

                propValues[0] = ResPropertyValue.createPropValue(nf.format(100 - rate), propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue(nf.format(rate), propMetas[1]);
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);

                return values;
            }

        } catch (Exception e) {
            logger.error("get informix sort disk rate information failed! ", e);
            throw new CollectorException(e);
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    logger.error("get informix sort disk rate information failed! ", e);
                    throw new CollectorException(e);
                }
        }
        return null;
    }

    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL" };
    }

    public String[] getPropMeta() {
        return new String[] { "MemorySortRate", "DiskSortRatio" };
    }

}
